iOS 9:
`if #available(iOS 11.0, *) {
} else {
navigationItemInit()
}`
`func navigationItemInit(){
self.createLeftBarBtnItem(iconstr: “”, title: “返回”, method: #selector(back))
}`
`func createLeftBarBtnItem(iconstr: String, title: String,method: Selector ) {
// 设置导航栏右侧按钮
let menuBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
menuBtn.addTarget(self, action: method, for: .touchUpInside)
if iconstr.count > 0 {
menuBtn.setImage(UIImage.init(named: iconstr), for: .normal)
}
if title.count > 0 {
menuBtn.setTitle(title, for: .normal)
}
let leftBtn = UIBarButtonItem(customView: menuBtn)
self.addFixedSpace(with: leftBtn, direction: .left)
}
func addFixedSpace(with barItem: UIBarButtonItem, direction: BarBtnItemDirection) {
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeSpacer.width = -11
switch direction {
case .left:
navigationItem.leftBarButtonItems = [negativeSpacer, barItem]
default:
navigationItem.rightBarButtonItems = [negativeSpacer, barItem]
}
}`
iOS 10.3.1:还需加上
`override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: false)
self.navigationController?.setNavigationBarHidden(false, animated: false)
}`